package com.hemerasolutions.v2trainer.model.schedule;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.util.Utility;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

@Entity
public class Availability extends BaseModel {

	private static final long serialVersionUID = 1L;

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY) 
    @JoinColumn(name = "monday_id") 	
	private List<TimeInterval> monday;

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY) 
    @JoinColumn(name = "tuesday_id") 	
	private List<TimeInterval> tuesday;

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY) 
    @JoinColumn(name = "wednesday_id") 	
	private List<TimeInterval> wednesday;

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY) 
    @JoinColumn(name = "thursday_id") 	
	private List<TimeInterval> thursday;

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY) 
    @JoinColumn(name = "friday_id") 	
	private List<TimeInterval> friday;

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY) 
    @JoinColumn(name = "saturday_id") 	
	private List<TimeInterval> saturday;

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY) 
    @JoinColumn(name = "sunday_id") 	
	private List<TimeInterval> sunday;

	public Availability() {
		super();
		this.monday = new ArrayList<TimeInterval>();
		this.tuesday = new ArrayList<TimeInterval>();
		this.wednesday = new ArrayList<TimeInterval>();
		this.thursday = new ArrayList<TimeInterval>();
		this.friday = new ArrayList<TimeInterval>();
		this.saturday = new ArrayList<TimeInterval>();
		this.sunday = new ArrayList<TimeInterval>();
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public List<TimeInterval> getMonday() {
		return monday;
	}

	public List<TimeInterval> getTuesday() {
		return tuesday;
	}

	public List<TimeInterval> getWednesday() {
		return wednesday;
	}

	public List<TimeInterval> getThursday() {
		return thursday;
	}

	public List<TimeInterval> getFriday() {
		return friday;
	}

	public List<TimeInterval> getSaturday() {
		return saturday;
	}

	public List<TimeInterval> getSunday() {
		return sunday;
	}

	public void setMonday(List<TimeInterval> monday) {
		this.monday = monday;
	}

	public void setTuesday(List<TimeInterval> tuesday) {
		this.tuesday = tuesday;
	}

	public void setWednesday(List<TimeInterval> wednesday) {
		this.wednesday = wednesday;
	}

	public void setThursday(List<TimeInterval> thursday) {
		this.thursday = thursday;
	}

	public void setFriday(List<TimeInterval> friday) {
		this.friday = friday;
	}

	public void setSaturday(List<TimeInterval> saturday) {
		this.saturday = saturday;
	}

	public void setSunday(List<TimeInterval> sunday) {
		this.sunday = sunday;
	}
	
	private String dayTime(List<TimeInterval> day){
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		DecimalFormat decFormat = new DecimalFormat("00");
		
		long totalTime = 0;

		try {
			if(day != null){
				
				for(TimeInterval interv:day){
					if(interv.getType() == 0 && interv.getEndTime() != null && interv.getBegTime() != null ){
						if(!(interv.getEndTime().compareTo(interv.getBegTime()) <= 0 )){
							Date d1 = format.parse(format.format(interv.getBegTime()));
							Date d2 = format.parse(format.format(interv.getEndTime()));
							totalTime = totalTime + ( d2.getTime() - d1.getTime());
						}	
					}else if(interv.getTotalTime() != null) {
							Date d3 = format.parse(format.format(interv.getTotalTime()));
							Date d4 = format.parse(format.format(Utility.begDayDateHour(interv.getTotalTime())));
							totalTime = totalTime + ( d3.getTime() - d4.getTime());
					}
				}
		
				long diffSeconds = totalTime / 1000 % 60;
				long diffMinutes = totalTime / (60 * 1000) % 60;
				long diffHours = totalTime / (60 * 60 * 1000) % 24;
		
				return decFormat.format(diffHours) + ":" + decFormat.format(diffMinutes) + ":" +  decFormat.format(diffSeconds);
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;		
	}
	

	public String getMondayTotalTime(){
		return this.dayTime(this.monday);
	}
	
	public String getTuesdayTotalTime(){
		return this.dayTime(this.tuesday);
	}
	public String getWednesdayTotalTime(){
		return this.dayTime(this.wednesday);
	}
	public String getThursdayTotalTime(){
		return this.dayTime(this.thursday);
	}
	public String getFridayTotalTime(){
		return this.dayTime(this.friday);
	}
	public String getSaturdayTotalTime(){
		return this.dayTime(this.saturday);
	}
	public String getSundayTotalTime(){
		return this.dayTime(this.sunday);
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((friday == null) ? 0 : friday.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((monday == null) ? 0 : monday.hashCode());
		result = prime * result
				+ ((saturday == null) ? 0 : saturday.hashCode());
		result = prime * result + ((sunday == null) ? 0 : sunday.hashCode());
		result = prime * result
				+ ((thursday == null) ? 0 : thursday.hashCode());
		result = prime * result + ((tuesday == null) ? 0 : tuesday.hashCode());
		result = prime * result
				+ ((wednesday == null) ? 0 : wednesday.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Availability other = (Availability) obj;
		if (friday == null) {
			if (other.friday != null)
				return false;
		} else if (!friday.equals(other.friday))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (monday == null) {
			if (other.monday != null)
				return false;
		} else if (!monday.equals(other.monday))
			return false;
		if (saturday == null) {
			if (other.saturday != null)
				return false;
		} else if (!saturday.equals(other.saturday))
			return false;
		if (sunday == null) {
			if (other.sunday != null)
				return false;
		} else if (!sunday.equals(other.sunday))
			return false;
		if (thursday == null) {
			if (other.thursday != null)
				return false;
		} else if (!thursday.equals(other.thursday))
			return false;
		if (tuesday == null) {
			if (other.tuesday != null)
				return false;
		} else if (!tuesday.equals(other.tuesday))
			return false;
		if (wednesday == null) {
			if (other.wednesday != null)
				return false;
		} else if (!wednesday.equals(other.wednesday))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Availability [id=" + id + ", monday=" + monday + ", tuesday="
				+ tuesday + ", wednesday=" + wednesday + ", thursday="
				+ thursday + ", friday=" + friday + ", saturday=" + saturday
				+ ", sunday=" + sunday + "]";
	}



	
	
}
